import java.io.FileReader;
import java.io.BufferedReader;

class Environment {
    SymbolManager symbolManager;
    SymbolDictionary dictionary;
    Heap heap;
    SmallTalkClass[] classes;
    
    public Environment(String file) throws Exception {
        BufferedReader reader = new BufferedReader(new FileReader(file));
        symbolManager = new SymbolManager(reader);
        dictionary = new SymbolDictionary(reader);
        heap = new Heap(reader);
        reader.readLine();
        classes = SmallTalkClass.readAll(reader);
        reader.close();
    }
    
    public String asEnvirFile() 
    {
        String ret = symbolManager.asString() + dictionary.asString("Smalltalk dictionary") + heap.toString();
        for(SmallTalkClass c: classes)
            ret = ret + c.asString();
        return ret;
    }
    public static void main(String[] args) throws Exception {
//        Environment env = new Environment("/users/Justin/compiler/envirfile.ini");
        Environment env = new Environment("/compiler/envirfile.ini");
        for(SmallTalkClass c: env.classes) {
            for(SmallTalkMethod meth: c.classMethods) {
                SemanticProcessor sp = new SemanticProcessor();
                CompiledMethod compiled = sp.compileMethod(meth.selector, meth.body);
                env.heap.createCompiledMethod(compiled);
            }
            //Duplicated code...better ways?
            for(SmallTalkMethod meth: c.instanceMethods) {
                SemanticProcessor sp = new SemanticProcessor();
                CompiledMethod compiled = sp.compileMethod(meth.selector, meth.body);
                env.heap.createCompiledMethod(compiled);
            }
        }
        env.heap.trimHeap();
        System.out.println(env.asEnvirFile());
    }
    
    //IO stuff...
    static void error(String msg) {
        System.out.println(msg);
        System.exit(-1);
    }
    
    static void warn(String msg) {
        System.out.println(msg);
    }
}
